﻿using _02;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace jiance
{
    public partial class Form2 : Form
    {
       DBHelper db = new DBHelper();
        public Form2()
        {
            InitializeComponent();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void Form2_Load(object sender, EventArgs e)
        {
            db.OpenConnection();
            string sql = "select * from StationInfo";
            SqlCommand comm= new SqlCommand(sql, db.Connection);
            SqlDataReader reader = comm.ExecuteReader();
            while (reader.Read())
            {
                this.comboBox1.Items.Add(reader["StationName"]);
            }
            //关闭
            reader.Close();
            db.CloseConnection();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.textBox1.Text = null;
            this.textBox5.Text = null;
            this.textBox4.Text = null;
            this.comboBox1.Text = null;

            this.textBox3.Text = null;
            this.textBox2.Text = null;
            this.textBox6.Text = null;

        }

        private void button1_Click(object sender, EventArgs e)
        {
            //录入非空判断
            if (this.textBox1.Text.Trim().Equals(string.Empty) ||
                this.textBox5.Text.Trim().Equals(string.Empty) ||
                this.textBox4.Text.Trim().Equals(string.Empty) ||
                this.comboBox1.Text.Trim().Equals(string.Empty) ||
                this.textBox6.Text.Trim().Equals(string.Empty) ||
                this.textBox3.Text.Trim().Equals(string.Empty) ||
                this.textBox2.Text.Trim().Equals(string.Empty))
            {
                MessageBox.Show("请输入完整");
            }
            else
            {
                db.OpenConnection();
                int one = 0;
                if (this.comboBox1.Text == "一号监测站")
                {
                    one = 1;
                }
                else if (this.comboBox1.Text == "二号监测站")
                {
                    one = 2;
                }
                else if (this.comboBox1.Text == "三号监测站")
                {
                    one = 3;
                }

                //SelectedValue 获得实际绑定值
                string sql = "insert into AirQualityInfo(StationID,InputName,StartDate,EndDate,PM,whi,Notes) values(N'" + one+ "',N'" +this.textBox2.Text + "',N'" + this.textBox3.Text + "',N'" + this.textBox4.Text + "',N'" + this.textBox5.Text + "',N'" + this.textBox6.Text + "',N'" + this.textBox1.Text + "')";
                SqlCommand command = new SqlCommand(sql, db.Connection);
                int coun = command.ExecuteNonQuery();

                if (coun > 0)
                {
                    MessageBox.Show("插入成功");
                    db.CloseConnection();
                    this.Close();
                }
                else
                {
                    MessageBox.Show("插入失败！");

                }
                db.CloseConnection();
            }
        }
    }
}
